using System;
using System.Collections.Generic;
using BasicScript.Component;
using Game.Main.Model;
using UnityEngine;

namespace Game.Component
{
	public class ClockBItem : BaseItem
	{
		private Transform num1Trans;

		private Transform num2Trans;

		private Dictionary<Transform, SpriteRenderer[]> numDic = new Dictionary<Transform, SpriteRenderer[]>();

		private float timer;

		private SpriteAssets spriteAssets;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			spriteAssets = model.GetComponent<SpriteAssets>();
			num1Trans = model.Find("Num1");
			num2Trans = model.Find("Num2");
			UpdateTime(DateTime.Now.Hour, DateTime.Now.Minute);
		}

		protected override void LateUpdate()
		{
			timer += Time.deltaTime;
			if (timer >= 60f)
			{
				timer = 0f;
				UpdateTime(DateTime.Now.Hour, DateTime.Now.Minute);
			}
		}

		public void SetNumber(int num, Transform numTrans)
		{
			string[] array = new string[2];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = num.ToString();
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
			}
			SpriteRenderer[] array2 = new SpriteRenderer[2];
			if (numDic.ContainsKey(numTrans))
			{
				array2 = numDic[numTrans];
			}
			else
			{
				array2 = new SpriteRenderer[2]
				{
					numTrans.Find("0").GetComponent<SpriteRenderer>(),
					numTrans.Find("1").GetComponent<SpriteRenderer>()
				};
				numDic.Add(numTrans, array2);
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				array2[i].sprite = spriteAssets.GetSprite(text);
			}
		}

		private void UpdateTime(float hourVal, float minuteVal)
		{
			SetNumber((int)hourVal, num1Trans);
			SetNumber((int)minuteVal, num2Trans);
		}
	}
}
